﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace _dmis.daily
{
    public class FtpCreateDir
    {
        List<string> listBuns = new List<string>();
        bool isBuns= false;
        
        /// <summary>
        /// 目录信息
        /// </summary>
        /// <param name="dirInfo">业主目录</param>
        public void CreateDir(string dirInfo)
        {
            string[] dirs = dirInfo.Split('_');

            for (int i = 0; i < dirs.Length; i++)
            {
                Uri uri = null;
                if (i == 0)
                {
                    uri = new Uri("ftp://192.168.1.120/" + dirs[i]);
                }
                else if (i == 1)
                {
                    uri = new Uri("ftp://192.168.1.120/" + dirs[i - 1] + "/" + dirs[i]);
                }
                else if (i == 2)
                {
                    uri = new Uri("ftp://192.168.1.120/" + dirs[i - 2] + "/" + dirs[i - 1] + "/" + dirs[i]);
                }

                FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(uri);

                listRequest.Method = WebRequestMethods.Ftp.MakeDirectory;

                string ftpUser = "anonymous";
                string ftpPassWord = "asf@sdfg.com";
                listRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

                FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse();
            }

            //MessageBox.Show(listResponse.StatusDescription);
        }

        /// <summary>
        /// 查询该目录是否存在
        /// </summary>
        /// <param name="dirInfo">业主目录</param>
        public void CommonMethod(string dirInfo)
        {
            string[] dirs = dirInfo.Split('_');

            Uri uri = new Uri("ftp://192.168.1.120/" + dirs[0] + "/" + dirs[1] + "/");

            FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(uri);

            //listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            listRequest.Method = WebRequestMethods.Ftp.ListDirectory;

            string ftpUser = "";
            string ftpPassWord = "";
            listRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

            FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse();
            Stream responseStream = listResponse.GetResponseStream();
            StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.Default);

            //if (readStream != null)
            //{
            //    string[] dirs = (readStream.ReadToEnd()).Split('\n');
            //    for (int i = 0; i < dirs.Length; i++)
            //    {
            //        MessageBox.Show(dirs[i]);
            //    }
            //}

            //MessageBox.Show(string.Format("状态: {0},{1}", listResponse.StatusCode, listResponse.StatusDescription));

            listResponse.Close();
            responseStream.Close();
        }
    }
}
